const experss = require("express")
const app = experss()
const history = require("connect-history-api-fallback")
const cors =require("cors")
// 日志
app.use(require("./apiLoggerMid"))


// app.use(history())
// 映射pinlic目录中的静态资源
const path = require('path')
const staticRoots = path.resolve(__dirname,"../public/ManagementEnd")
console.log("staticRoots",staticRoots);
app.use("/ManagementEnd",experss.static(staticRoots,{
    index:"index.html"
}))

const staticRoot = path.resolve(__dirname,"../public/StudentEnd")
app.use("/",experss.static(staticRoot,{
    index:"index.html"
}))








app.get('/upload/*', function (req, res) {
    res.sendFile(path.resolve(__dirname,"../public" + req.url));

})



const whileList = ["http://localhost:5173","http://127.0.0.1:5500","http://localhost:8080"] // 白名单
app.use(
    cors({
      origin(origin, callback) {
        if (whileList.includes(origin) || !origin) {
          callback(null, origin);
        } else {
          callback(new Error("not allowed"));
        }
        //callback(null,origin)
      },
      credentials: true,
    })
  );

// 解析 application/x-www-form-urlencoded 格式的请求体
app.use(experss.urlencoded({
    extended:true
}))
// 解析 application/json 格式的请求体
app.use(experss.json())


app.use(require("./tokenMiddleware"))
// 学生接口
app.use("/api/student",require("./api/student"))
// 通用接口
app.use("/api/currency",require("./api/currency"))
// 管理员
app.use("/api/admin",require("./api/admin"))
// 超级管理员
app.use("/api/super",require("./api/super"))






app.use(require("./errorMiddleware"))


app.listen(8080,()=>{
    console.log("server listen on 8080");
})